package com.xintoucloud.pbs.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xintoucloud.pbs.entity.MeetingPicture;
import com.xintoucloud.pbs.service.MeetingPictureService;

@Controller
@RequestMapping("file")
public class FileComtroller {

	@Autowired
	private MeetingPictureService meetingPictureService;

	@RequestMapping("/into")
	public String uploading() {
		return "file";
	}

	@RequestMapping("/uploadFile")
	@ResponseBody
	public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws FileNotFoundException, IOException {
		
		String fileName = file.getOriginalFilename(); 
		System.out.println("fileName+++++++++++"+fileName);
		String type = file.getContentType();
		System.out.println("type=========="+type);
		String path = request.getServletContext().getRealPath("images");
		System.out.println("path======"+path);
		IOUtils.copy(file.getInputStream(),new FileOutputStream(path+"/"+fileName));
		
		MeetingPicture meetingPicture = new MeetingPicture();
		meetingPicture.setUrl(fileName);
		meetingPicture.setMeetingNum("001");
		meetingPictureService.save(meetingPicture);
		Map<String, Object> param = new HashMap<>();
		param.put("code", 0);
		param.put("msg", "success");
		Map<String, Object> data = new HashMap<>();
		data.put("src", "image/aaaa.jpg");
		param.put("data", data);
		return param;
	}
	/*@RequestMapping("/uploadFile")
	@ResponseBody
	public List<FileDto> upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws FileNotFoundException, IOException{
		
		String name = file.getOriginalFilename();
		System.out.println("============="+name);
		String type = file.getContentType();
		
		String path = request.getServletContext().getRealPath("images");
		IOUtils.copy(file.getInputStream(),new FileOutputStream(path+"/"+name));
		List<FileDto> fileDtos = new ArrayList<>();
		FileDto fileDto = new FileDto();
		fileDto.setCode(0);
		fileDto.setMsg("success");
		DataDto dataDto = new DataDto();
		dataDto.setSrc("image/aaaa.jpg");
		fileDto.setData(dataDto);
		fileDtos.add(fileDto);
	
		return fileDtos;
	}*/

}
